var express = require("express");
var router = express.Router();
const db = require("../model/db");
var multiparty = require("multiparty");
// 获取门店信息
router.get("/store", function (req, res, next) {
  const sql = "SELECT * FROM store";
  db.query(sql, function (err, data) {
    res.send({
      data,
      code: 200,
    });
  });
});
// 获取车辆和门店信息搜索车辆信息
router.get("/vsearch", function (req, res, next) {
  let {name,vstatus,sid} = req.query
  console.log(name,vstatus,sid);
  let sql = `SELECT * FROM vehicle,store where vehicle.sid = store.id`;
  if (name) {
    sql += ` AND vname LIKE '%${name}%'`; 
  }
  if (vstatus) {
    sql += ` AND vstatus = '${vstatus}'`;
  }
  if (sid) {
    sql += ` AND sid = ${sid}`;
  }
  console.log(sql,'1231465');
  db.query(sql, function (err, data) {
    res.send({
      data,
      code: 200,
    });
  });
});
// 上传图片
router.post("/upload", (req, res) => {
  let form = new multiparty.Form();
  form.uploadDir = "upload";
  form.parse(req, (err, formData, imgData) => {
    res.send({
      code: 200,
      path:"http://localhost:3100/" + imgData.image[0].path,
    });
  });
});
// 添加车辆信息
router.post("/Cvehicle", function (req, res, next) {
  let body = req.body;
  console.log(body);
  let filename = body.previewImage;
  let filename1 = body.previewImage1;
  let filename2 = body.previewImage2;
  let data = {
    ...body.values,
    image: filename,
    inspectannuallyProve: filename1,
    annualInsuranceImg: filename2,
  }
  const sql = `INSERT INTO vehicle (${Object.keys(data)}) values ('${Object.values(data).join("','")}')`;
  console.log(sql);
  db.query(
    sql,
    function (err, result) {
      res.send({
        message: "添加成功",
        result: result,
        code: 200,
      });
    }
  );
});
// 修改车辆信息
router.post("/Update", function (req, res, next) {  
  let body = req.body;  
  let id = body.id; 
  let filename = body.previewImage;  
  let filename1 = body.previewImage1;  
  let filename2 = body.previewImage2;  
  console.log(body);
  let data = {  
    ...body.values,  
  };  
  if (filename) {
    data.image = filename;
  }
  if(filename1){
    data.inspectannuallyProve = filename1;
  }
  if(filename2){
    data.annualInsuranceImg = filename2;
  }
  const sql = `UPDATE vehicle SET ${Object.keys(data).map(key => `${key}='${data[key]}'`).join(", ")} WHERE veid='${id}'`;  
  console.log(sql);
  db.query(  
    sql,  
    function (err, result) {  
      if (err) {  
        res.status(500).send({ message: "修改失败", code: 500 });  
      } else {  
        res.send({ message: "修改成功", result: result, code: 200 });  
      }  
    }  
  );  
});
// 删除车辆信息
router.post("/Dvehicle", function (req, res, next) {
  const id = req.body.id;
  console.log(id);
  const sql = `Delete from vehicle where veid=${id}`;
  console.log(sql);
  db.query(sql, function (err, data) {
    res.send({
      code: 200,
    });
  });
});
// 获取车型信息/搜索
router.get("/vesearch", function (req, res, next) {
  let {brand,year,seats,boxtype,displacement,TransmissionType}  = req.query
  console.log(brand,'brand');
  const whereList = []
  if (brand) {
    whereList.push(`brand = '${brand}'`)
  }
  if(year){
    whereList.push(`year = '${year}'`)
  }
  if(seats){
    whereList.push(`seats = '${seats}'`)
  }
  if(boxtype){
    whereList.push(`boxtype = '${boxtype}'`)
  }
  if(displacement){
    whereList.push(`displacement = '${displacement}'`)
  }
  if(TransmissionType){
    whereList.push(`TransmissionType = '${TransmissionType}'`)
  }
  let q = `SELECT * FROM vehiclemodel`;
  if (whereList.length > 0) {
    q += ' where ' + whereList.join(' AND ')
  }
  console.log(q,'q');
  db.query(q, function (err, data) {
    res.send({
      data,
      code: 200,
    });
  });
});
// 添加车型信息
router.post("/Cvehiclemodel", function (req, res, next) {
  let body = req.body;
  let filename = body.previewImage;
  let data = {
    ...body.values,
    cximage: filename,
  }
  const sql = `INSERT INTO vehiclemodel (${Object.keys(data)}) values ('${Object.values(data).join("','")}')`;
  db.query(
    sql,
    function (err, result) {
      res.send({
        message: "添加成功",
        result: result,
        code: 200,
      });
    }
  );
});
// 修改车型信息
router.post("/Uvehiclemodel", function (req, res, next) {  
  let body = req.body;  
  console.log(body);  
  let id = body.ids; 
  let filename = body.previewImage; 
  let data = {  
    ...body.values,  
  };  
  if (filename) {
    data.cximage = filename;
  }
  const sql = `UPDATE vehiclemodel SET ${Object.keys(data).map(key => `${key}='${data[key]}'`).join(", ")} WHERE cid='${id}'`;  
  db.query(  
    sql, 
    function (err, result) {  
      if (err) {  
        res.status(500).send({ message: "修改失败", code: 500 });  
      } else {  
        res.send({ message: "修改成功", result: result, code: 200 });  
      }  
    }  
  );  
});
// 删除车型信息
router.post("/Dvehiclemodel", function (req, res, next) {
  const id = req.body.id;
  console.log(id);
  const sql = `Delete from vehiclemodel where cid=${id}`;
  console.log(sql);
  db.query(sql, function (err, data) {
    res.send({
      code: 200,
    });
  });
});
// 获取门店信息
router.get("/User", function (req, res, next) {
  const sql = "SELECT * FROM Users";
  db.query(sql, function (err, data) {
    res.send({
      data,
      code: 200,
    });
  });
});
//修改调度人员
router.post("/Uvehicle", function (req, res, next) {  
  let body = req.body;  
  let data = {  
    ...body.values,  
  };  
  let id = body.id; 
  console.log(body);
  const sql = `UPDATE vehicle SET ${Object.keys(data).map(key => `${key}='${data[key]}'`).join(", ")} WHERE veid='${id}'`;  
  console.log(sql);
  db.query(  
    sql,  
    function (err, result) {  
      if (err) {  
        res.status(500).send({ message: "修改失败", code: 500 });  
      } else {  
        res.send({ message: "修改成功", result: result, code: 200 });  
      }  
    }  
  );  
});
// 修改租金
router.post("/Urent", function (req, res, next) {  
  let body = req.body;  
  let data = {  
    ...body.values,  
  };  
  let id = body.id; 
  console.log(body);
  const sql = `UPDATE vehicle SET ${Object.keys(data).map(key => `${key}='${data[key]}'`).join(", ")} WHERE veid='${id}'`;  
  db.query(  
    sql,  
    function (err, result) {  
      if (err) {  
        res.status(500).send({ message: "修改失败", code: 500 });  
      } else {  
        res.send({ message: "修改成功", result: result, code: 200 });  
      }  
    }  
  );  
});
module.exports = router;
